<?php


namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\Exception;

class Veridisk extends Backend
{
    protected $model = null;
    protected $noVeriDisk = ['index'];
    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['index'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Veridisk;
    }

    public function index(){
        if ($this->haveVeriDisk()&&$this->rightVeriDisk()) {
            $this->success("服务器合法", url('index/login'));
        }
        $getDiskId = getDiskId();
        if ($this->request->isPost()) {
            $token = trim($this->request->post('token'));
            if(veriDiskId($getDiskId)!=$token){
                $this->error("秘钥有误,请重新输入", url('veridisk/index'));
            }
            Db::startTrans();
            try{
                $this->model->where('id',1)->update(['token'=>$token]);
                Db::commit();
            }catch(Exception $e){
                Db::rollback();
                $this->error("操作失败",url('veridisk/index'));
            }
            $this->success("秘钥正确", url('index/login'));
        }
        //硬盘序列号
        $this->view->assign('diskid', $getDiskId);
        return $this->view->fetch();
    }


    //在线获取token
    public function gettoken() {
        if ($this->request->isPost()) {
            $diskid = trim($this->request->post('diskid'));
            $token = veriDiskId($diskid);
            //发送钉钉消息
            $this->model->sendMsgMarkdown($token);
            return json(["code"=>1,"msg"=>"获取成功","data"=>$token]);
        }
        return $this->view->fetch();
    }


}
